// ShellExtAssistent.h : Declaration of the CFilerFrogShellExt

#pragma once
#include "resource.h"       // main symbols

#include "FilerFrog_i.h"
#include <shlobj.h>
#include <comdef.h>

#include "..\FilesRefactor\FilesRefactor.h"

#include "MenuItemsHolder.h"
#include "RecentActionsManager.h"
#include "FilerFrogSettings.h"
#include "ExplorerController.h"
#include "FilerFrogDialogs.h"

#include <string>
#include <list>
#include <map>
#include <gdiplus.h>

#undef INTSAFE_E_ARITHMETIC_OVERFLOW

using namespace std;
using namespace Gdiplus;

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif


class ATL_NO_VTABLE CFilerFrogShellExt:
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CFilerFrogShellExt, &CLSID_FilerFrogShellExt>,
	public IShellExtInit,
	public IContextMenu3,
	public IObjectWithSiteImpl<CFilerFrogShellExt>,
	public IReportProgess
{
protected:
	FRRESPONSE m_LastResult;
	bool m_IsVistaOs;
	bool m_IsRightClickOnDesktop;

	// File Operations
	list<wstring> m_FilesList;
	//FilesRefactor m_FilesRefactor;
	ExplorerController m_ExplorerController;

	// Recent Histoy
	RecentActionsManager m_RecentActionsManager;

	// Menu Command Map
	map<UINT, FileOps> m_MenuCommandMap;

	// Menu Items
	static MenuItemsHolder s_MenuItemsHolder;

	FilerFrogSettings m_FilerFrogSettings;

	// Path of current folder
	wstring m_FolderPath;

	// List
	wstring m_ListFilePath;
	bool m_IsHtml;
	bool m_IncludeSubfolders;
	bool m_Indent;	

	FileOps m_CurrentOperation;	

	bool m_NeedsRefresh;

	// Progress Dialog
	CFrogProgressDialog m_FrogProgressDialog;

	// IReportProgess Methods
	virtual void ReportProgress(int progress, bool closeProgressWindow);
	virtual void ExecuteCommand();	
	void ExecuteCommandWithProgressBar(HWND parent);	

	// Error Message
	void HandleErrorMesseages(FRRESPONSE result, HWND parent);

	// Context Menu
	void GetDisabledActionsList(list<FileOps>& disabledOpsList);
	bool IsInDisabledActionsList(FileOps item, list<FileOps>& disabledOpsList);
	HMENU AddMenuSubItem(HMENU hParentMenu, HMENU subMenu, const wstring& name, UINT cmd, UINT pos, MenuPics iconIndex, bool disabled = false);
	HMENU AddOperationMenu(const HMENU parentMenu,  UINT& idCmd, const UINT uidFirstCmd, UINT& pos, const FileOps operation, bool disabled = false, bool shortDescription = false);
	void AddSortDesktopMenu(HMENU& mainMenu, UINT& idCmd, UINT& pos, UINT& uidFirstCmd);	

	// Dialog Helper
	void ShowImageResizeFastDialog(HICON currentCommandIcon, HWND hwnd, double shrinkBy, bool saveAsJpeg);
public:

	DECLARE_REGISTRY_RESOURCEID(IDR_FILERFROGSHELLEXT)

	DECLARE_NOT_AGGREGATABLE(CFilerFrogShellExt)

	CFilerFrogShellExt();
	virtual ~CFilerFrogShellExt();

	void ExternalDecryptFileDialog(const wstring& filePath);
	void ExternalJoinFileDialog(const wstring& filePath);

	BEGIN_COM_MAP(CFilerFrogShellExt)
		COM_INTERFACE_ENTRY(IShellExtInit)
		COM_INTERFACE_ENTRY(IContextMenu)
		COM_INTERFACE_ENTRY(IContextMenu2)
		COM_INTERFACE_ENTRY(IContextMenu3)
		COM_INTERFACE_ENTRY(IObjectWithSite)
	END_COM_MAP()

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}

	// IShellExtInit
	STDMETHODIMP Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY);

	// IContextMenu2
	STDMETHODIMP GetCommandString(UINT_PTR, UINT, UINT*, LPSTR, UINT);
	STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO);
	STDMETHODIMP QueryContextMenu(HMENU, UINT, UINT, UINT, UINT);	
	STDMETHODIMP HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam);
	// IContextMenu3
	STDMETHODIMP HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *pResult);
};

OBJECT_ENTRY_AUTO(__uuidof(FilerFrogShellExt), CFilerFrogShellExt)